package cn.witsky.rtp2ws;

import java.io.File;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.witsky.rtp2ws.util.FileUtil;
import io.netty.util.internal.StringUtil;

public class Server {
	private static Logger logger = LoggerFactory.getLogger(Server.class);
	private static Thread clearBackUpFileThread=null;
	private static boolean compress=false;
	public static boolean signature = true;
	public static String backUpRtpFileDir = "rtp2ws-file";
	public static int udpHeadLen = 12;
	public static String downLoadDir = "rtp2ws-downLoad";
	public static String retryLog = "rtp2ws-retry.log";
	public static String ffmpegBinPath = "ffmpeg";
	private static boolean saveFile = false;
	public static boolean sendWsSync = false;
	public static int serverId;
	public static boolean reusePort = false;
	public static String serverType = "rtp2ws";
	public static String ServerName;
	public static int THRED_COUNT = Runtime.getRuntime().availableProcessors();
	public static long MAX_RUNNING_TIME = 3600 * 2 * 1000;
	public static int RING_BUFFER_SIZE = 1024;
	public static boolean useProxy = true;
	public static boolean useNewThread = false;
	public static int retryMax = 4;
	public static int saveFileDays=3;
	private static String ip;
	private static String port;
	public static String endFlag = "EOS";
	private static int rtpSpeed = 0;
	private static int payLoadLen = 0;
	private static int waiTime = 0;
	private static int onceSendBytelen = 0;
	private static String serverInfo;
	private static String appKey = "";
	private static String appId = "";
	private static String addedInfo = "";

	public static int getPayLoadLen() {
		return payLoadLen;
	}

	public static int getWaiTime() {
		return waiTime;
	}

	public static void setRtpSpeed(String rtpSpeedStr) {
		if (!StringUtil.isNullOrEmpty(rtpSpeedStr)) {
			Server.rtpSpeed = Integer.parseInt(rtpSpeedStr);
			if (rtpSpeed > 20) {
				onceSendBytelen = 1;
				waiTime = rtpSpeed;
				payLoadLen = (rtpSpeed / 20) * 160;
			} else {
				onceSendBytelen = 2;
				waiTime = 40;
			}
		}
	}

	public static void setPayLoad(String payLoadStr) {
		if (!StringUtil.isNullOrEmpty(payLoadStr)) {
			Server.payLoadLen = Integer.parseInt(payLoadStr);
		}
	}

	public static int getOnceSendBytelen() {
		return onceSendBytelen;
	}

	public static int getRtpSpeed() {
		return rtpSpeed;
	}

	public static int additiveHash(String key) {
		int hash, i;
		for (hash = key.length(), i = 0; i < key.length(); i++)
			hash += key.charAt(i);
		return (hash % Server.THRED_COUNT);
	}

	public static String getServerInfo() {
		return serverInfo;
	}

	public static void setServerInfo(String serverInfo) {
		Server.serverInfo = serverInfo;
	}

	public static String getAppKey() {
		return appKey;
	}

	public static void setAppKey(String appKey) {
		Server.appKey = appKey;
	}

	public static String getAppId() {
		return appId;
	}

	public static void setAppId(String appId) {
		Server.appId = appId;
	}

	public static int getServerId() {
		return serverId;
	}

	public static void setServerId(int serverId) {
		Server.serverId = serverId;
		Server.ServerName = serverType + "-" + serverId;
	}

	public static String getAddedInfo() {
		return addedInfo;
	}

	public static void setAddedInfo(String addedInfo) {
		Server.addedInfo = addedInfo;
	}

//	public static String getIp() {
//		return ip;
//	}

	public static void setIp(String ip) {
		Server.ip = ip;
	}

//	public static String getPort() {
//		return port;
//	}

	public static void setPort(String port) {
		Server.port = port;
	}

	public static void setReusePort(String reuseUdpPort) {
		if (!StringUtil.isNullOrEmpty(reuseUdpPort) && reuseUdpPort.equals("1")) {
			Server.reusePort = true;
		}
	}

	public static void setSendWsSync(String sendWsSyncStr) {
		if (!StringUtil.isNullOrEmpty(sendWsSyncStr) && sendWsSyncStr.equals("1")) {
			Server.sendWsSync = true;
		}
	}

	public static String getHost() {
		return Server.ip + ":" + Server.port;
	}

	public static boolean isSaveFile() {
		return saveFile;
	}

	public static void setSaveFile(boolean saveFile) {
		Server.saveFile = saveFile;
		try {
			if (saveFile) {
				File backFileDir = new File(backUpRtpFileDir);
				if (!backFileDir.exists()) {
					backFileDir.mkdir();
				}
				clearBackUpFileThread=new Thread(new Runnable() {					
					@Override
					public void run() {
						int days=saveFileDays;
						while(true) {
							try {								
								boolean isSuc=FileUtil.clearFile(Server.backUpRtpFileDir,days);
								if(!isSuc) {
									logger.warn("执行目录:{}文件清理失败",Server.backUpRtpFileDir);
								}
								isSuc=FileUtil.clearFile(Server.downLoadDir,1);
								if(!isSuc) {
									logger.warn("执行目录:{}文件清理失败",Server.downLoadDir);
								}
								TimeUnit.HOURS.sleep(1);
							}catch(Exception ex) {
								logger.error("清理文件线程执行异常:",ex);
							}
						}						
					}
				});
				clearBackUpFileThread.setName("rtp2ws-clearBackUpFile-thread");
			}
			File downLoadFileDir = new File(downLoadDir);
			if (!downLoadFileDir.exists()) {
				downLoadFileDir.mkdir();
			}
		} catch (Exception ex) {

		}
	}

	public static String getFfmpegBinPath() {
		return ffmpegBinPath;
	}

	public static void setFfmpegBinPath(String ffmpegBinPath) {
		Server.ffmpegBinPath = ffmpegBinPath;
	}

	public static boolean isSignature() {
		return signature;
	}

	public static void setSignature(boolean signature) {
		Server.signature = signature;
	}

	public static void setSaveFileDays(int saveFileDays) {
		if(saveFileDays>0) {
			Server.saveFileDays = saveFileDays;
		}
	}

	public static boolean isCompress() {
		return compress;
	}

	public static void setCompress(boolean compress) {
		Server.compress = compress;
	}

}
